/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package btnhom;

import conn.JavaConnectDb;
import java.sql.Connection;
import javax.swing.JOptionPane;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;

/**
 *
 * @author lhqbao
 */
public class Duong {
    private Integer ma_duong;
    private String ten_duong;
    
    Connection conn;
    OraclePreparedStatement pst;
    OracleResultSet rs;

    public Integer getMa_duong() {
        return ma_duong;
    }

    public void setMa_duong(Integer ma_duong) {
        this.ma_duong = ma_duong;
    }

    public String getTen_duong() {
        return ten_duong;
    }

    public void setTen_duong(String ten_duong) {
        this.ten_duong = ten_duong;
    }
    
    public OracleResultSet SelectDuong(){
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "select * from duong";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            rs = (OracleResultSet) pst.executeQuery();
            
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Khong the ket noi server");
        }
        return rs;
        
    } 
    
    public void insertDuong(String maduong, String tenduong)
    {
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "insert into duong(ma_duong, ten_duong) values(?,?)";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(1, maduong);
            pst.setString(2, tenduong);
            pst.executeQuery();
                    
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Khong the ket noi server khi cap nhat");
        }finally {
        //try { rs.close(); } catch (Exception e) { /* ignored */ }
       // try { pst.close(); } catch (Exception e) { /* ignored */ }
        try { conn.close(); } catch (Exception e) { /* ignored */ }
        }
    }
    
    public void updateDuong(String maduong, String tenduong)
    {
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "update duong set ten_duong=? where ma_duong=?";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(2, maduong);
            pst.setString(1, tenduong);
            pst.executeQuery();
            JOptionPane.showMessageDialog(null, "Cap nhat thanh cong");        
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Cap nhat khong thanh cong");
        } finally {
       // try { rs.close(); } catch (Exception e) { /* ignored */ }
        //try { pst.close(); } catch (Exception e) { /* ignored */ }
        try { conn.close(); } catch (Exception e) { /* ignored */ }
        }
    }
    
    public void deleteDuong(String maduong)
    {
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "delete from duong where ma_duong=?";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(1, maduong);
            pst.executeQuery();
            JOptionPane.showMessageDialog(null, "Xoa thanh cong");        
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Xoa khong thanh cong");
        } finally {
        //try { rs.close(); } catch (Exception e) { /* ignored */ }
        //try { pst.close(); } catch (Exception e) { /* ignored */ }
        try { conn.close(); } catch (Exception e) { /* ignored */ }
        }
    }
}
